import * as types from './mutation-types'
import { playMode } from 'common/js/config'
import { shuffle } from 'common/js/util'
import { saveSearch, deleteSearch, clearSearch, savePlay, saveFavorite, deleteFavorite } from 'common/js/cache'

function findIndex (list, song) {
  return list.findIndex((item, index) => {
    return item.id === song.id
  })
}

// 用户点击播放歌曲
export const selectPlay = function ({ commit, state }, { list, index }) {
  // 顺序播放列表
  commit(types.SET_SEQUENCE_LIST, list)
  if (state.mode === playMode.random) {
    let randomList = shuffle(list)
    commit(types.SET_PLAYLIST, randomList)
    index = findIndex(randomList, list[index])
  } else {
    commit(types.SET_PLAYLIST, list)
  }

  commit(types.SET_CURRENT_INDEX, index)
  commit(types.SET_PLAYING_STATE, true)
  commit(types.SET_FULL_SCREEN, true)
}

// 用户点击 music-list 随机播放按钮
export const randomPlay = function ({ commit, state }, { list }) {
  // 播放模式 随机播放
  commit(types.SET_PLAY_MODE, playMode.random)
  // 顺序播放列表
  commit(types.SET_SEQUENCE_LIST, list)
  // 洗牌数组
  let randomList = shuffle(list)
  commit(types.SET_PLAYLIST, randomList)
  commit(types.SET_PLAYING_STATE, true)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_CURRENT_INDEX, 0)
}

// 插入歌曲
export const insertSong = function ({ commit, state }, song) {
  // 播放列表 修改副本 房子报错
  let playlist = state.playlist.slice()
  // 顺序列表
  let sequenceList = state.sequenceList.slice()
  // 当前播放索引
  let currentIndex = state.currentIndex
  // 当前播放歌曲
  let currentSong = playlist[currentIndex]
  // 先查找我们插入的歌曲在不在我们的播放列表
  let fpIndex = findIndex(playlist, song)
  // 我们要插入的位置是当前播放歌曲索引的下一个
  currentIndex++
  // 插入歌曲
  playlist.splice(currentIndex, 0, song)
  // 判断 大于-1 表示已经有这首歌曲
  if (fpIndex > -1) {
    // 如果要插入的歌曲在当前播放歌曲之后[1,2,3,4,2]
    if (currentIndex > fpIndex) {
      // 如果已经包含  我们就要删除歌曲
      playlist.splice(fpIndex, 1)
      currentIndex--
    } else if (currentIndex < fpIndex) {
      // 如果要插入的歌曲在当前播放歌曲之前[4,8,5,6,7,8]
      playlist.splice(fpIndex + 1, 1)
    }
  }

  // sequenceList应该要插入的位置
  let currentSIndex = findIndex(sequenceList, currentSong) + 1
  // 我们查找一下 sequenceList 有没有包含要插入的 song
  let fsIndex = findIndex(sequenceList, song)
  // sequenceList 插入歌曲
  sequenceList.splice(currentSIndex, 0, song)
  // 判断
  if (fsIndex > -1) {
    if (currentSIndex > fsIndex) {
      // 插入在后面  直接删除之前的index
      sequenceList.splice(fsIndex, 1)
    } else if (currentSIndex < fsIndex) {
      // 插入在前面
      sequenceList.splice(fsIndex + 1, 1)
    }
  }

  commit(types.SET_PLAYLIST, playlist)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)
  // 播放状态 true
  commit(types.SET_PLAYING_STATE, true)
  // 播放器展开状态 true
  commit(types.SET_FULL_SCREEN, true)
}

// 搜索历史
export const saveSearchHistory = function ({commit, state}, query) {
  commit(types.SET_SEARCH_HISTORY, saveSearch(query))
}

// 用户点击 单个删除 搜索历史
export const deleteSearchHistory = function ({commit, state}, query) {
  commit(types.SET_SEARCH_HISTORY, deleteSearch(query))
}

export const clearSearchHistory = function ({commit, state}) {
  commit(types.SET_SEARCH_HISTORY, clearSearch())
}

// 删除歌单中的歌曲
export const deleteSong = function ({commit, state}, song) {
  let playlist = state.playlist.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex

  let pIndex = findIndex(playlist, song)
  playlist.splice(pIndex, 1)

  let sIndex = findIndex(sequenceList, song)
  sequenceList.splice(sIndex, 1)

  // 当前播放的歌曲在删除歌曲的后面 或是正在播放的歌曲是在当前播放歌单的最后一首
  if (currentIndex > pIndex || currentIndex === playlist.length) {
    currentIndex--
  }

  commit(types.SET_PLAYLIST, playlist)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)

  // 判断 整个列表全部删除 停止播放
  playlist.length === 0 ? commit(types.SET_PLAYING_STATE, false) : commit(types.SET_PLAYING_STATE, true)
}

// 用户点击删除全部歌曲按钮
export const deleteSongList = function ({commit, state}) {
  commit(types.SET_PLAYLIST, [])
  commit(types.SET_SEQUENCE_LIST, [])
  commit(types.SET_CURRENT_INDEX, -1)
  commit(types.SET_PLAYING_STATE, false)
}

// 存储播放过的歌曲
export const savePlayHistory = function ({commit, state}, song) {
  commit(types.SET_PLAY_HISTORY, savePlay(song))
}

// 用户点击收藏歌曲
export const saveFavoriteList = function ({commit, state}, song) {
  commit(types.SET_FAVORITE_LIST, saveFavorite(song))
}

// 用户再次点击删除收藏
export const deleteFavoriteList = function ({commit, state}, song) {
  commit(types.SET_FAVORITE_LIST, deleteFavorite(song))
}
